import 'package:flutter/material.dart';
import 'package:flutter_bmfmap_example/models/orderinfomodel.dart';
import 'package:flutter_bmfmap_example/models/ordermodel.dart';
import 'package:flutter_bmfmap_example/pages/marketpage/componets/gooditemcard.dart';
import 'package:flutter_bmfmap_example/models/collectioninfomodel.dart';
import 'package:flutter_bmfmap_example/pages/marketpage/pages/orderpage/page/Evaluate.dart';
import 'package:flutter_bmfmap_example/pages/marketpage/pages/paypage/paypage.dart';
import 'package:flutter_bmfmap_example/service/marketservice.dart';
import 'package:provider/provider.dart';

import 'gooditemcard.dart';

class OrdersListview extends StatefulWidget {
  OrdersListview({Key key,this.type}) : super(key: key);

  int type;

  @override
  _OrdersListviewState createState() => _OrdersListviewState();
}

class _OrdersListviewState extends State<OrdersListview> {
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: context.watch<Orderinfomodel>().getorderlistbytype(widget.type).length,
      itemBuilder: (context, index) {
        return OrderItemCard(
            orderItem:context.watch<Orderinfomodel>().getorderlistbytype(widget.type)[index]);
      },
    );
  }
}

class OrderItemCard extends StatefulWidget {
  OrderItemCard({ Key key ,this.orderItem}) : super(key: key);

  int index;
  OrderItem orderItem;

  @override
  _OrderItemCardState createState() => _OrderItemCardState();
}

class _OrderItemCardState extends State<OrderItemCard> {
  @override
  Widget build(BuildContext context) {
    String price = widget.orderItem.money.toString();
    String state = "揽货中";
    String confirm = "取消订单";
    String id = widget.orderItem.id;
    if (widget.orderItem.state == 0) {
      state = "未付款";
    } else if (widget.orderItem.state == 1) {
      state = "待发货";
    } else if (widget.orderItem.state == 2) {
      state = "待签收";
      confirm = "确认收货";
    } else if (widget.orderItem.state == 3) {
      state = "待评价";
      confirm = "去评价";
    } else if (widget.orderItem.state == 4) {
      state = "已评价";
      confirm = "追加评价";
    }

    var num = 0;
    for (int i = 0; i < widget.orderItem.orderitems.length; i++) {
      num += int.parse(widget.orderItem.orderitems[i]['commodityNumber']);
    }

    return Container(
      margin: EdgeInsets.fromLTRB(20, 20, 20, 0),
      padding: EdgeInsets.all(8),
      decoration: BoxDecoration(
          color: Colors.white70,
          borderRadius: BorderRadius.circular(10),
          boxShadow: [
            BoxShadow(
                color: Colors.grey[300],
                blurRadius: 18,
                spreadRadius: 0,
                offset: Offset(0, 6))
          ]),
      child: Container(
        child: Column(
          children: [
            Container(
              margin: EdgeInsets.fromLTRB(0, 0, 0, 8),
              child: Row(
                children: [
                  Container(
                      margin: EdgeInsets.only(right: 4),
                      decoration: BoxDecoration(
                        border: Border.all(color: Colors.black26, width: 0.5),
                        borderRadius: BorderRadius.circular(16),
                        gradient: LinearGradient(
                            begin: Alignment.topLeft,
                            end: Alignment.bottomRight,
                            // stops: [0.0,1.0],
                            colors: [Colors.pink, Colors.pinkAccent]),
                      ),
                      child: Center(
                        child: Image.asset(
                          'assets/images/common/logo.png',
                          width: 32,
                          height: 32,
                        ),
                      )),
                  Text('Nest',
                      style: TextStyle(
                        fontWeight: FontWeight.w500,
                        color: Colors.black87,
                      )),
                  Spacer(),
                  Container(
                    margin: EdgeInsets.only(right: 8),
                    child: Text(
                      state,
                      style: TextStyle(
                          color: Colors.pink,
                          fontWeight: FontWeight.w600,
                          fontSize: 12),
                    ),
                  )
                ],
              ),
            ),
            Column(
              children: _builditemlist(widget.orderItem.orderitems),
            ),
            Divider(),
            Container(
              // color: Colors.grey[300],
              child: Row(
                children: [
                  Container(
                    margin: EdgeInsets.only(left: 4),
                    child: Text(
                      '共 ' + num.toString() + ' 件',
                      style: TextStyle(color: Colors.black54, fontSize: 12),
                    ),
                  ),
                  Spacer(),
                  Container(
                    margin: EdgeInsets.only(right: 16),
                    child: Text(
                      '总价 ￥$price',
                      style: TextStyle(
                          color: Colors.lightBlueAccent[600], fontSize: 13),
                    ),
                  ),
                  if(widget.orderItem.state==0)Row(
                    children: [
                      _buildpaybutton(),
                      SizedBox(width:8),
                      _buildcancelbutton(),
                    ],
                  ),
                  if(widget.orderItem.state==1)_buildcancelbutton(),
                  if(widget.orderItem.state==2)_buildexpressbutton(),
                  if(widget.orderItem.state==3)_buildevaluatebutton(),
                  if(widget.orderItem.state==4)_buildevaluatebutton(),
                  // InkWell(
                  //   child: Container(
                  //     padding: EdgeInsets.fromLTRB(8, 4, 8, 4),
                  //     decoration: BoxDecoration(
                  //       border: Border.all(color: Colors.black26, width: 0.5),
                  //       borderRadius: BorderRadius.circular(12),
                  //       gradient: LinearGradient(
                  //           begin: Alignment.topLeft,
                  //           end: Alignment.bottomRight,
                  //           // stops: [0.0,1.0],
                  //           colors: [Colors.pink, Colors.pinkAccent]),
                  //     ),
                  //     child: Text(
                  //       '取消订单',
                  //       style: TextStyle(fontSize: 12, color: Colors.white),
                  //     ),
                  //   ),
                  //   // onTap: () async {
                  //   //   var isOk = false;
                  //   //   String Authorization =
                  //   //       "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJkb25nZG9uZzNAcXEuY29tIiwiaWF0IjoxNjI2OTM0OTg1LCJleHAiOjE2Mjc1Mzk3ODV9.OGrQRW-2WBoUiO85uMm3Ab8ajhVG5Jvt2LGptzwPQaK9fNhFdFGYB9SDjaV9VWGONc9AiKN2xXUZoQcTAGPvZg";
                  //   //   if (confirm == "取消订单") {
                  //   //     print("取消订单");
                  //   //     this.list.removeAt(index);
                  //   //     String url =
                  //   //         "http://xiangrikui.vaiwan.com/orderForm/delete";
                  //   //     var dio = new Dio();
                  //   //     dio.options.headers["Authorization"] = Authorization;
                  //   //     Response response = await dio
                  //   //         .post(url, queryParameters: {"id": item["id"]});
                  //   //     print(response.data);
                  //   //     isOk = response.data;
                  //   //     reGetAllList();
                  //   //   } else if (confirm == "确认收货") {
                  //   //     print('确认收货');
                  //   //     String url =
                  //   //         "http://xiangrikui.vaiwan.com/orderForm/confirm";
                  //   //     var dio = new Dio();
                  //   //     dio.options.headers["Authorization"] = Authorization;
                  //   //     Response response = await dio
                  //   //         .post(url, queryParameters: {"id": item["id"]});
                  //   //     print(response.data);
                  //   //     isOk = response.data;
                  //   //     reGetAllList();
                  //   //   } else if (confirm == "去评价" || confirm == "追加评价") {
                  //   //     print("评价");
                  //   //     reGetAllList();
                  //   //     Navigator.push(context, new MaterialPageRoute(
                  //   //         builder: (BuildContext context) {
                  //   //       return new Evalute(item: item);
                  //   //     }));
                  //   //   }
                  //   //   if (confirm == "确认收货" || confirm == "取消订单") {
                  //   //     if (isOk != false) {
                  //   //       Fluttertoast.showToast(
                  //   //           msg: "操作成功",
                  //   //           toastLength: Toast.LENGTH_SHORT,
                  //   //           gravity: ToastGravity.BOTTOM,
                  //   //           timeInSecForIosWeb: 1,
                  //   //           backgroundColor: Colors.black12,
                  //   //           textColor: Colors.green[600],
                  //   //           fontSize: 16.0);
                  //   //     } else {
                  //   //       Fluttertoast.showToast(
                  //   //         msg: "操作失败",
                  //   //         toastLength: Toast.LENGTH_SHORT,
                  //   //         gravity: ToastGravity.CENTER,
                  //   //         timeInSecForIosWeb: 1,
                  //   //         backgroundColor: Colors.black38,
                  //   //         textColor: Colors.red[600],
                  //   //         fontSize: 16.0,
                  //   //         // webPosition: ToastGravity.CENTER
                  //   //       );
                  //   //     }
                  //   // }
                  //   // }
                  // )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  _buildcancelbutton(){
    return InkWell(
      onTap: () async {
      //   Navigator.push(context, MaterialPageRoute(
      //   builder: (context) => PayPage(id:widget.orderItem.id)
      // ));
      await Marketservice.deleteorder(widget.orderItem.id);
      context.read<Orderinfomodel>().init();
      },
      child: Container(
        padding: EdgeInsets.fromLTRB(8, 4, 8, 4),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(12),
          gradient: LinearGradient(
              begin: Alignment.topLeft, end: Alignment.bottomRight,
              colors: [Colors.pink, Colors.pinkAccent]),
        ),
        child: Text(
          '取消订单',
          style: TextStyle(fontSize: 12, color: Colors.white),
        ),
      ),
    );
  }

  _buildpaybutton() {
    return InkWell(
      onTap: (){
        Navigator.push(context, MaterialPageRoute(
        builder: (context) => PayPage(id:widget.orderItem.id)
      ));
      },
      child: Container(
        padding: EdgeInsets.fromLTRB(8, 4, 8, 4),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(12),
          gradient: LinearGradient(
              begin: Alignment.topLeft, end: Alignment.bottomRight,
              colors: [Colors.pink, Colors.pinkAccent]),
        ),
        child: Text(
          '付款',
          style: TextStyle(fontSize: 12, color: Colors.white),
        ),
      ),
    );
  }

  _buildevaluatebutton(){
    return InkWell(
      onTap: (){
        Navigator.push(context, MaterialPageRoute(
        builder: (context) => EvalutePage(
          orderItem: widget.orderItem,
        )
      ));
      },
      child: Container(
        padding: EdgeInsets.fromLTRB(8, 4, 8, 4),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(12),
          gradient: LinearGradient(
              begin: Alignment.topLeft, end: Alignment.bottomRight,
              colors: [Colors.pink, Colors.pinkAccent]),
        ),
        child: Text(
          '评价',
          style: TextStyle(fontSize: 12, color: Colors.white),
        ),
      ),
    );
  }

  _buildexpressbutton() {
    return InkWell(
      onTap: (){
        Navigator.push(context, MaterialPageRoute(
        builder: (context) => PayPage(id:widget.orderItem.id)
      ));
      },
      child: Container(
        padding: EdgeInsets.fromLTRB(8, 4, 8, 4),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(12),
          gradient: LinearGradient(
              begin: Alignment.topLeft, end: Alignment.bottomRight,
              colors: [Colors.pink, Colors.pinkAccent]),
        ),
        child: Text(
          '查看物流',
          style: TextStyle(fontSize: 12, color: Colors.white),
        ),
      ),
    );
  }

  _builditemlist(List orderitems) {
    List<Widget> list = [];
    for (int i = 0; i < orderitems.length; i++) {
      list.add(Padding(
        padding: const EdgeInsets.only(bottom: 16),
        child: OrderGooditemCard(
          commodityId: orderitems[i]['commodityId'],
          num: orderitems[i]['commodityNumber'],
          intro: orderitems[i]['intro'],
          money: orderitems[i]['money'],
          name: orderitems[i]['name'],
          imgurl1: orderitems[i]['imgurl1'],
        ),
      ));
    }
    return list;
  }
}


  

